package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.bean.Install3Bean;
import com.wnhz.work.yz.utils.GlideImageLoader;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.youth.banner.Banner;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class InstallDetails2Activity extends BaseActivity implements View.OnClickListener {
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install_details2);
        EventBus.getDefault().register(this);
      //  MyApplication.getInstance().addDestoryActivity(this, "install2");
        initTitle();

        initView();
        upLoad();
    }


    private Install3Bean.InfoEntity info = new Install3Bean.InfoEntity();
    private String type;

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("pid", pid);
        map.put("type", type);
        showDialog();
        XUtil.Post(Url.DECORATE_DETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        Install3Bean bean = gson.fromJson(result, Install3Bean.class);
                        info = bean.getInfo();
                        setData();
                    } else {
                        String info = jsonObject.optString("info");
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });

    }

    private Banner banner;
    private TextView names, price;
    private RecyclerView youxiu;
    private EditText shop;
    int num = 1;

    private void initView() {
        findViewById(R.id.btn).setOnClickListener(this);
        findViewById(R.id.reduce).setOnClickListener(this);
        findViewById(R.id.ll_add).setOnClickListener(this);
        banner = (Banner) findViewById(R.id.banner);
        price = (TextView) findViewById(R.id.price);
        names = (TextView) findViewById(R.id.names);
        youxiu = (RecyclerView) findViewById(R.id.youxiu);
        youxiu.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        shop = (EditText) findViewById(R.id.shop);
        shop.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    num = Integer.parseInt(shop.getText().toString());
                } catch (Exception e) {
                    num = 0;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void setData() {
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(info.getIndex_pic());
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        price.setText("￥" + info.getPrice() + "/" + info.getUnit());
        names.setText(info.getName() + "");
        youxiu.setAdapter(new BaseRVAdapter(this, info.getExample()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_youxiu;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                ImageLoader.getInstance().displayImage(info.getExample().get(position).getImg(), holder.getImageView(R.id.bg12312));
                holder.setTextView(R.id.name, "业主：" + info.getExample().get(position).getName());
                holder.setTextView(R.id.address, info.getExample().get(position).getAddress() + "\t\t" + info.getExample().get(position).getArea());
            }

        });
    }

    private String pid = "";
    private String name = "";

    private void initTitle() {
        type = getIntent().getStringExtra("type");
        pid = getIntent().getStringExtra("pid");
        name = getIntent().getStringExtra("name");
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("" + name);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        ImageView ic_telephone = (ImageView) findViewById(R.id.ic_telephone);
        ic_telephone.setOnClickListener(this);
        ic_telephone.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.ic_telephone:
                break;
            case R.id.reduce:
                if (num <= 0) {
                    MyToast("再减就没了");
                    return;
                }
                num--;
                shop.setText(num + "");

                break;
            case R.id.ll_add:
                num++;
                shop.setText(num + "");
                break;
            case R.id.btn:
                if (MyApplication.getInstance().getUserBean() == null) {
                    MyToast("请登录");
                    startActivity(new Intent(getApplicationContext(), LoginFirstActivity.class));
                    return;
                }
                if (num == 0) {
                    MyToast("至少选择1套");
                    return;
                }
                startActivity(new Intent(this, InstallYuYue3Activity.class).putExtra("type", "0").putExtra("pid", pid).putExtra("type", info.getType()).putExtra("price", info.getPrice()).putExtra("num", num));
                break;

        }
    }

}
